'use strict';

/**
 * @param {Egg.Application} app - egg application
 */
module.exports = app => {
  const { router, controller, middleware } = app;
  router.get('/', controller.home.index);
  router.get('/user', controller.user.index);
  router.get('/news', controller.news.list);
 
  // 验证 Authorization
  const authMiddleware = middleware.auth();
  // Get Current User
  // 如果全局config.middleware注册了该中间件，这里也会再出发一次 
  // router.post('/api/user', authMiddleware, controller.user.getCurregntUser);
  // eggjs的put路由，controller不会被触发
  // Get Current User
  router.get('/api/user', controller.user.getCurregntUser);
  // Update User
  router.put('/api/user', controller.user.updateUser);
  // Get Profile  
  router.get('/api/profiles/:username', controller.user.getProfile);
  // Follow user
  router.post('/api/profiles/:username/follow', controller.user.followUser);
  // Unfollow user
  router.delete('/api/profiles/:username/follow', controller.user.unfollowUser);
  // Create Article
  router.post('/api/articles', controller.article.createArticle);
  // List Articles
  router.get('/api/articles', controller.article.listArticles);
  // Feed Articles为什么路由要写在Get Article前面
  // 在实际开发中，路由的顺序非常重要。如果路由的顺序不正确，可能会导致某些路由无法访问，或者访问到错误的路由。
  // Feed Articles 用于获取当前用户所关注的作者发布的信息
  router.get('/api/articles/feed', controller.article.feedArticles);
  // Get Article
  router.get('/api/articles/:slug', controller.article.getArticle);
  // Update Article
  router.put('/api/articles/:slug', controller.article.updateArticle);
  // Delete Article
  router.delete('/api/articles/:slug', controller.article.deleteArticle);
  // Add Comments to an Article
  router.post('/api/articles/:slug/comments', controller.article.addComments);
  // Get Comments from an Article
  router.get('/api/articles/:slug/comments', controller.article.getComments);
  // Delete Comment
  router.delete('/api/articles/:slug/comments/:id', controller.article.deleteComment);
  // Favorite Article
  router.post('/api/articles/:slug/favorite', controller.article.favoriteArticle);
  // Unfavorite Article
  router.delete('/api/articles/:slug/favorite', controller.article.unfavoriteArticle);
  // Get Tags
  router.get('/api/tags', controller.tag.getTags);

  // console.log(app.router.stack) // 查看已定义的路由
  // console.log(app.middleware) //查看已定义的中间件
  // 为了保证路由的正确性和安全性，
  // 需要将需要进行身份验证的路由放在不需要身份验证的路由之前，以避免出现安全漏洞和访问异常等问题。
   // Registration
   router.post('/api/users', controller.user.registration);
   // Authentication
   router.post('/api/users/login', controller.user.login);
};
